from datetime import datetime
from common.json_util import to_dicts
from daos.system_user_dao import *
from models.biz_enum import AccountRole, AccountStatus, SystemLogModule
from rules import db_commit, PAGINATION_PAGE_SIZE
from rules.system_log_rule import create_system_log


def search_system_user(pk):
    result = query_system_user(pk)
    return result


def search_system_user_by_username(username):
    result = query_system_user_by_username(username)
    return result


def search_system_user_with_page(page_index):
    data_param = dict(page=page_index, page_size=PAGINATION_PAGE_SIZE)
    result = query_system_user_with_page(data_param)
    item_dicts = to_dicts(result.items)
    for item in item_dicts:
        item["role_name"] = AccountRole.get_name(item.get("role"))
        item["status_name"] = AccountStatus.get_name(item.get("status"))

    result.items = item_dicts
    return result


def create_system_user(form, current_user):
    is_commit = False

    user_orm = SystemUser()

    user_orm.username = form.username.data
    user_orm.password = form.password.data
    user_orm.real_name = form.real_name.data
    user_orm.role = form.role.data
    user_orm.status = AccountStatus.NORMAL.value

    result = add_system_user(user_orm, is_commit)

    # 添加系统日志
    operation_detail_dict = dict(before_operation=None, after_operation=user_orm.to_dict())
    log_dict = dict(module=SystemLogModule.CREATE_SYSTEM_USER, operation_detail=operation_detail_dict,
                    operator=current_user.id)
    create_system_log(log_dict, is_commit)

    if not is_commit:
        db_commit()

    return result.to_dict()


def describe_system_user(pk, form, current_user):
    is_commit = False
    now = datetime.now()
    user_orm = query_system_user(pk)
    if user_orm:
        before_operation = user_orm.to_dict()

        need_update_list = ["last_update_time"]
        if form.real_name.data != user_orm.real_name:
            user_orm.real_name = form.real_name.data
            need_update_list.append("real_name")

        if form.password.data:
            user_orm.password = form.password.data
            need_update_list.append("password_hash")

        if form.role.data != user_orm.role:
            user_orm.role = form.role.data
            need_update_list.append("role")

        user_orm.last_update_time = now

        update_system_user(user_orm, *need_update_list, is_commit=is_commit)

        # 添加系统日志
        operation_detail_dict = dict(before_operation=before_operation, after_operation=user_orm.to_dict())
        log_dict = dict(module=SystemLogModule.UPDATE_SYSTEM_USER, operation_detail=operation_detail_dict,
                        operator=current_user.id)
        create_system_log(log_dict, is_commit)

        if not is_commit:
            db_commit()

    return


def describe_system_user_status(pk, current_user):
    is_commit = False
    user_orm = query_system_user(pk)
    if user_orm:
        before_operation = user_orm.to_dict()

        need_update_list = ["status", "last_update_time"]
        if user_orm.status == AccountStatus.NORMAL.value:
            user_orm.status = AccountStatus.DISABLED.value

            module = SystemLogModule.DISABLED_SYSTEM_USER
        else:
            user_orm.status = AccountStatus.NORMAL.value

            module = SystemLogModule.ENABLED_SYSTEM_USER

        update_system_user(user_orm, *need_update_list, is_commit=is_commit)

        # 添加系统日志
        operation_detail_dict = dict(before_operation=before_operation, after_operation=user_orm.to_dict())
        log_dict = dict(module=module, operation_detail=operation_detail_dict, operator=current_user.id)
        create_system_log(log_dict, is_commit)

        if not is_commit:
            db_commit()

    return
